# LED support for Raspberry Pi, /sys based version
#
# Copyright (C) 2013 Anselm Lingnau <anselm@anselms.net>
#
# This is not exactly efficient but does the job without root privileges.
# Use the "led" command to allow access to the gpio control files.

import time

_COLOR_FILE_NAME = "/sys/class/gpio/gpio%u/value"

_color_file = []
for pin in (4, 17, 27):
    _color_file.append(open(_COLOR_FILE_NAME % pin, "w"))

_color = {
    'r': (1,0,0), 'g': (0,1,0), 'b': (0,0,1), # red, green, blue
    'y': (1,1,0), 'c': (0,1,1), 'm': (1,0,1), # yellow, cyan, magenta
    'x': (0,0,0), 'w': (1,1,1),
}

def color(c):
    c = _color[c[0].lower()]
    for i in range(3):
        print >>_color_file[i], c[i]
        _color_file[i].flush()

def blink(s, step=0.2):
    for c in s:
        color(c)
        time.sleep(step)
